using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls.Adapters;

public partial class EnterEvent : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DataSet ds = new DataSet();
        ManagePerson mp = new ManagePerson();
        ds = mp.getAllPeopleAtLibrary(int.Parse(Request.QueryString["id"]));
        ddlContact.DataSource = ds;
        ddlContact.DataTextField = "Name";
        ddlContact.DataValueField = "PersonId";
        ddlContact.DataBind();
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        pnlInsert.Visible = false;
        lblMessage.Text = "";

        DataTable dt = new DataTable();
        GridView_selected.DataSource = dt;
        GridView_selected.DataBind();

        GridView_Events.DataSource = SqlDataSource_Search;
        GridView_Events.DataBind();

        
        
        //try
        //{
        //    lblMessage.Text = "";
        //    ManageEvent mEvent = new ManageEvent();
        //    SqlDataAdapter sqlDat = mEvent.searchEventLibrarianView(txtSearch.Text, int.Parse(this.Request.QueryString.Get("id")));
        //    DataSet d = new DataSet();
        //    sqlDat.Fill(d);
        //    GridView_Events.DataSource = d;
        //    GridView_Events.DataBind();

        //    if (GridView_Events.Rows.Count < 1)
        //    {
        //        lblMessage.Text = "There are no events that match your search";
        //    }
        //}
        //catch (Exception ex)
        //{
        //    lblMessage.Text = ex.Message.ToString();
        //}
    }
    protected void GridView_Events_SelectedIndexChanged(object sender, EventArgs e)
    {
        //ManageEvent me = new ManageEvent();
        //Event anEvent = new Event();
        //int eventId = int.Parse(GridView_Events.SelectedDataKey.Value.ToString());
        //anEvent = me.getEvent(eventId);

        DetailsView_Event.ChangeMode(DetailsViewMode.Edit);
        //pnlUpdate.Visible = true;
        GridView_selected.DataSource = SqlDataSource_selected;
        DetailsView_Event.DataSource = SqlDataSource_selected;
        //anEvent.Title = txtTitle.Text;
        //anEvent.Description = txtDescription.Text;
        //anEvent.Location = txtLocation.Text;
        //DateTime dateTime = dateTimeSelector.Date;
        //anEvent.dateTime = dateTime;
        //anEvent.dateTime = dateTimeSelector.Year;
        //anEvent.dateTime.Month = dateTimeSelector.Month;
        //dateTimeSelector.Day = anEvent.dateTime.Day;
        //dateTimeSelector.Hour = anEvent.dateTime.Hour;
        //dateTimeSelector.Minute = anEvent.dateTime.Minute;

   

        GridView_selected.DataBind();
        DetailsView_Event.DataBind();

        DataTable dt = new DataTable();
        GridView_Events.DataSource = dt;
        GridView_Events.DataBind();

        

        //String s = GridView_Events.SelectedRow.Cells[0].Text.ToString().Trim();
        //ManageEvent mEvent = new ManageEvent();
        //SqlDataAdapter sqlDat = mEvent.searchEventLibrarianView(s, int.Parse(this.Request.QueryString.Get("id")));
        //DataSet d = new DataSet();
        //sqlDat.Fill(d);
        //GridView_Events.DataSource = d;  
 
        //GridViewRow gvRow = GridView_Events.SelectedRow;
        //DataTable dt = new DataTable();
        //dt.ImportRow((DataRow)gvRow.DataItem);
        //GridView_Events.DataSource = dt;
        //GridView_Events.DataBind();
        
    }

    protected void dateTimeSelector_DateChanged(object sender, EventArgs e)
    {
        //ManageEvent me = new ManageEvent();
        //Event anEvent = new Event();
        //int eventId = int.Parse(GridView_Events.SelectedDataKey.Value.ToString());
        //anEvent = me.getEvent(eventId);

        //dateTimeSelector.Year = anEvent.dateTime.Year;
        //dateTimeSelector.Month = anEvent.dateTime.Month;
        //dateTimeSelector.Day = anEvent.dateTime.Day;
        //dateTimeSelector.Hour = anEvent.dateTime.Hour;
        //dateTimeSelector.Minute = anEvent.dateTime.Minute;
    }

    protected void DetailsView_Event_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
    {
        if (e.Exception != null)
        {
            lblMessage.Text = "A database error has occured. <br /><br /> Message: " + e.Exception.Message;
            e.ExceptionHandled = true;
            e.KeepInEditMode = true;
        }
        else if (e.AffectedRows == 0)
        {
            lblMessage.Text = "Another user may have updated that record. <br /> Please try again.";
            e.ExceptionHandled = true;
            e.KeepInEditMode = true;
        }
        else
        {
            lblMessage.Text = "Your record has been saved";
            GridView_selected.DataBind();
        }
    }

    protected void DetailsView_Event_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
    {
        try
        {
            lblMessage.Text = "";
            int eventId = int.Parse(DetailsView_Event.DataKey.Value.ToString().Trim());
            string Title = DetailsView_Event.Rows[0].Cells[1].Controls[0].ToString().Trim();
            string Description = DetailsView_Event.Rows[1].Cells[1].Controls[0].ToString().Trim();
            DateTime DateTime = Convert.ToDateTime(DetailsView_Event.Rows[2].Cells[1].Controls[0].ToString());
            string Location = DetailsView_Event.Rows[3].Cells[1].Controls[0].ToString().Trim();
            ManageEvent mEvent = new ManageEvent();
            int affectedRows = mEvent.updateEventLibrarianView(eventId, Title, Description, DateTime, Location);
            
            if (affectedRows < 1)
                lblMessage.Text = "Your update was not saved";
            else
                lblMessage.Text = "Your update was saved";

        }
        catch (Exception ex)
        {
            lblMessage.Text = ex.Message.ToString();
        }
        //ManageEvent me = new ManageEvent();

        //DetailsView_Event.DataSource = SqlDataSource_selected;
        //String key = DetailsView_Event.DataKey.Value.ToString();

        //SqlDataSource_selected.UpdateParameters.Add("EventId", System.Data.DbType.Int32, key);
        //SqlDataSource_selected.UpdateParameters.Add("Title", System.Data.DbType.String, DetailsView_Event.Rows[0].Cells[0].Text.ToString().Trim());
        //SqlDataSource_selected.UpdateParameters.Add("Description", System.Data.DbType.String, DetailsView_Event.Rows[0].Cells[1].Text.ToString().Trim());
        //SqlDataSource_selected.UpdateParameters.Add("dateTime", System.Data.DbType.DateTime, DetailsView_Event.Rows[0].Cells[2].Text);
        //SqlDataSource_selected.UpdateParameters.Add("Location", System.Data.DbType.String, DetailsView_Event.Rows[0].Cells[3].Text.ToString().Trim());
        //SqlDataSource_selected.Update();


    }
    protected void btnNew_Click(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        GridView_selected.DataSource = dt;
        GridView_selected.DataBind();
        GridView_Events.DataSource = dt;
        GridView_Events.DataBind();
        DetailsView_Event.DataSource = dt;
        DetailsView_Event.DataBind();

        lblMessage.Text = "";
        pnlInsert.Visible = true;

        //DetailsView_Event.ChangeMode(DetailsViewMode.Insert);
    }

    protected void lnkSave_OnClick(object sender, EventArgs e)
    {
        try
        {
            ManageEvent me = new ManageEvent();
            Event anEvent = new Event();

            anEvent.ContactID = int.Parse(ddlContact.SelectedValue);
            anEvent.Title = txtTitle.Text;
            anEvent.Description = txtDescription.Text;
            anEvent.Location = txtLocation.Text;
            DateTime dateTime = dateTimeSelector.Date;
            anEvent.dateTime = dateTime;
            anEvent.LibraryID = int.Parse(Request.QueryString["id"]);

            int affectedRows = me.enterEvent(anEvent);

            if (affectedRows < 1)
                lblMessage.Text = "Please try again.";
            else
                lblMessage.Text = "Saved";
        }
        catch (Exception ex)
        {
            lblMessage.Text = ex.Message.ToString();
        }
    }
    protected void DetailsView_Event_PageIndexChanging(object sender, DetailsViewPageEventArgs e)
    {
        
    }

    protected void DetailsView_Event_OnModeChanging(object sender, DetailsViewModeEventArgs e)
    {
        if (e.CancelingEdit)
        {
            lblMessage.Text = "";

            DataTable dt = new DataTable();   
            DetailsView_Event.DataSource = dt;
            DetailsView_Event.DataBind();

            GridView_selected.DataSource = dt;
            GridView_selected.DataBind();

            GridView_Events.DataSource = SqlDataSource_Search;
            GridView_Events.DataBind();
        }
    }
}
